<template>
  <div id='rol'>
    <v-titletop>
      <span
        class='titleh'>角色名:</span>
      <el-input
        placeholder="请输入内容">
      </el-input>
      <span
        class='titleh'>角色ID:</span>
      <el-input
        placeholder="请输入内容">
      </el-input>
      <el-button
        type="primary">查找
      </el-button>
    </v-titletop>
    <v-titlebottom>
      <el-button
        type="primary"
        @click='add'>
        创建角色
      </el-button>
      <template style='auto'>
        <el-table
          :data="rollist"
          height="auto" border
          style="width: 98%">
          <el-table-column
            label="角色ID"
            prop='_id'
            width="180">
          </el-table-column>
          <el-table-column
            label="角色名称"
            prop="name"
            width="180">
          </el-table-column>

          <el-table-column
            prop='auth_time'
            label="授权时间">
          </el-table-column>
          <el-table-column
            prop='auth_name'
            label="授权人">
          </el-table-column>
          <el-table-column
            label="操作">
            <template
              slot-scope="scope">
              <el-button
                size="mini"
                @click="handleEdit(scope.$index, scope.row)">
                修改
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </v-titlebottom>
    <keep-alive>
      <component :is='comname'
        v-if='flag'
        @func='send'
        @get='getrollist'
        :cominfo='cominfo'
        :rolinfo=rolinfo>
        <h4>{{cominfo}}</h4>
      </component>
    </keep-alive>
  </div>
</template>

<script>
export default {
  name: 'rol',
  components: {
    'v-titletop': () => import('@/components/title-top'),
    'v-titlebottom': () => import('@/components/title-bottom'),
    updatarol: () => import('@/components/commons/updatarol'),
  },
  data() {
    return {
      rollist: [],
      flag: false,
      cominfo: '',
    }
  },
  created() {
    this.getrollist()
  },
  methods: {
    getrollist() {
      this.$axios({
        method: 'get',
        url: '/manage/role/list',
      }).then((res) => {
        let data = res.data.data
        for (var i = 0; i < data.length; i++) {
          let time = new Date(data[i].auth_time)
          let str = ''
          str += time.getFullYear() + '-'
          str += time.getMonth() + 1 + '-'
          str += time.getDate() + ' '
          str += time.getHours() + ':'
          str += time.getMinutes() < 10 ? '0' + time.getMinutes() : time.getMinutes()
          if (str == 'NaN-NaN-NaN NaN:NaN') {
            str = ''
          }
          data[i].auth_time = str
        }
        this.rollist = res.data.data
      })
    },
    send(data) {
      this.flag = data
    },
    add() {
      this.$prompt('请输入角色名', '添加角色')
        .then(({ value }) => {
          if (value) {
            this.$axios({
              method: 'post',
              url: '/manage/role/add',
              headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
              },
              params: {
                roleName: value,
              },
            }).then((res) => {
              if (res.data.status == 0) {
                this.$message.success('添加成功')
                this.$emit('func', this.flag)
                this.getrollist()
              } else if (res.data.status == 1) {
                this.$message.error('角色已存在')
              }
            })
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入',
          })
        })
    },
    change(index, row) {
      this.flag = true
      this.comname = 'updatarol'
      this.cominfo = '更新角色'
      this.$store.state.rolinfo = row
    },
    handleEdit(index, row) {
      this.flag = true
      this.comname = 'updatarol'
      this.cominfo = '修改权限'
      this.rolinfo = row
    },
  },
}
</script>

<style>
#rol {
  padding: 10px;
  box-sizing: border-box;
  position: absolute;
  width: 100%;
}
</style>